using System.Web;
using System.Web.Security;
using NHibernate;
using Web.DataAccess;
using Web.Model;

namespace Web.Extensions
{
	public interface IUserSession
	{
		void LogIn(User user);
		void LogOut();
		User CurrentUser { get; }
		bool IsAuthenticated { get; }
	}

	public class UserSession : IUserSession
	{
		public UserSession(ISession session)
		{
			var email = HttpContext.Current.User.Identity.Name;
			//IsAuthenticated = !string.IsNullOrEmpty(email);

			CurrentUser = new UserByEmailQuery(email).Load(session);
            IsAuthenticated = CurrentUser != null;
		}

		public void LogIn(User user)
		{
			FormsAuthentication.SetAuthCookie(user.Email, true);
		}

		public void LogOut()
		{
			FormsAuthentication.SignOut();
		}

		public User CurrentUser { get; private set; }

		public bool IsAuthenticated { get; private set; }
	}
}